
package al.components;



import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.Popup;

import org.jdesktop.swingx.JXFrame;
import org.jdesktop.swingx.JXTreeTable;
import org.jdesktop.swingx.treetable.AbstractMutableTreeTableNode;
import org.jdesktop.swingx.treetable.DefaultTreeTableModel;

import al.jdbc.DAO.Gestion_courrierDAO;
import al.jdbc.DAO.OrganigrammeDAO;
import al.jdbc.dpbridge.MySqlDataSource;

@SuppressWarnings("serial")
public class OrgD extends JPanel implements MouseListener{
	
	JPanel contentB;
	//JPanel contentR;
	//Form f;
	public int nbrCols;
	private JScrollPane scroll;
	private JButton validate=new JButton("Valider");
	private JXTreeTable tree;
	private DefaultTreeTableModel mdl;
	private final String[] headings = { "Organigramme","id"};
	private OrganigrammeDAO cdao = Gestion_courrierDAO.getInstance().getOrganigrammeDao();
	private int nivCount = 0;
	private DefaultListModel listModel;
	private JTextField text;
	private OrganigrammeNode n1 ;
	private boolean hasListModel = false;
	private boolean hasTextField = false;
	private Popup popup;
	private JFrame frame;
	private JButton launcher;
	
	public OrgD(){
		this.setLayout(new BorderLayout());
		this.setBackground(Color.WHITE);
		
		contentB = new JPanel();
		//contentB.setLayout(new BorderLayout());
		contentB.setBackground(Color.WHITE);
		nivCount = cdao.getNivCount();
		n1 = new OrganigrammeNode(cdao.findAll().get(0));
		/*n1.add(new Node(new Object[]{cdao.findAll().get(1)}));
		n1.add(new Node(new Object[]{cdao.findAll().get(2)}));
		n1.setUserObject(new Object[]{cdao.findAll().get(3)});*/
		
		loadData(n1);
		
		mdl = new DefaultTreeTableModel(n1, Arrays.asList(headings));
		tree = new JXTreeTable(mdl);
		scroll = new JScrollPane(tree);
		
		tree.setShowGrid(true, true);
		tree.setRootVisible(true);
		tree.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		tree.setColumnControlVisible(true);
		tree.setHorizontalScrollEnabled(true);
		tree.setFillsViewportHeight(false);
		tree.expandRow(0);
		tree.packAll();
		tree.addMouseListener(this);
		contentB.add(scroll);	
		this.add("Center",contentB);
	}
	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					JXFrame frame = new JXFrame();
					OrgD fc = new OrgD();
					fc.setVisible(true);
					frame.getContentPane().add(new JScrollPane(fc));
					frame.pack();
					frame.setVisible(true);
					frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	public void setListModel(DefaultListModel mo){
		this.listModel = mo;
		hasListModel = true;
		hasTextField = false;
	}
	public void setTextField(JTextField tf){
		this.text = tf;
		hasTextField = true;
		hasListModel = false;
	}
	public void setPopup(Popup p){
		this.popup = p;
	}
	public void setFrame(JFrame frame){
		this.frame = frame;
	}
	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		if(e.getClickCount() == 2){
			alOmrane.jdbc.beans.Organigramme m = cdao.findById((Integer)tree.getValueAt(tree.getSelectedRow(), 1));
			if(hasListModel){
				if(!listModel.contains(m))
				listModel.addElement(m);
			}else if(hasTextField ){
				text.setText(m.getId()+"");
				launcher.setEnabled(true);
				popup.hide();
			}
		}
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	private class Node extends AbstractMutableTreeTableNode{
		public Node(Object [] data) {
			super(data);
		}
		@Override
		public int getColumnCount() {
			return getData().length;
		}
		@Override
		public Object getValueAt(int column) {
			return getData()[column];
		}

		public Object[] getData() {
			return (Object[]) super.getUserObject();
		}
	}
	private class OrganigrammeNode extends Node{

		public OrganigrammeNode(alOmrane.jdbc.beans.Organigramme data) {
			super(new Object[]{data,data.getId()});
		}
		
		public List<OrganigrammeNode> getChilds(){
			if(cdao.hasChilds((alOmrane.jdbc.beans.Organigramme)super.getData()[0])){
				List<alOmrane.jdbc.beans.Organigramme> orgs = cdao.getChilds((alOmrane.jdbc.beans.Organigramme)super.getData()[0]);
				List<OrganigrammeNode> childs = new ArrayList<OrganigrammeNode> ();
				if(orgs != null ) {
					for(int i=0;i<orgs.size();i++)
					childs.add(new OrganigrammeNode(orgs.get(i)));
					return childs;
				}else return null;
			}
			return null;
		}
	}
	public void loadData(OrganigrammeNode on){
		List<OrganigrammeNode> ons = on.getChilds();
		if(ons != null){
			for(int i=0;i<ons.size();i++){
				on.add(ons.get(i));
			}
			for(int i=0;i<ons.size();i++){
				loadData(ons.get(i));
			}
		}
	}

	public void setLauncher(JButton b) {
		this.launcher = b;
	}
}

